/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-11-22
 * Time: 23:48
 */
public class Solution {
    public int search(int[] nums, int target) {
        int length=nums.length;
        int left=0;
        int right=length-1;
        while(left<=right){
            int mid=(left+right)>>1;
            if(nums[mid]==target){
                return mid;
            }
            if(nums[mid]<target){
                //直接判断什么时候位于左半边(这样是为了代码更简便,分支更少了)
                //注意这里必须带上等号
                if(nums[left]>=nums[mid]&&nums[right]<target){
                    right=mid-1;
                }else{
                    left=mid+1;
                }
            }else{
                //直接判断什么时候位于右半边
                //注意这里必须带上等号
                if(nums[left]<=nums[mid]&&nums[left]>target){
                    left=mid+1;
                }else{
                    right=mid-1;
                }
            }
        }
        return -1;
    }
}

